package client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import server.LocationInterface;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

import javax.swing.*;

public class SubTicket extends JDialog {

	private final JPanel contentPanel = new JPanel();
	Person person = new Person();
	private String name;

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		try {
			SubTicket dialog = new SubTicket(null,0,false,0);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	/**
	 * Create the dialog.
	 */
	public SubTicket(String a,double b,boolean c, final int d) {
		
		setBounds(100, 100, 500, 350);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(new Color(255, 255, 255));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
			
			try{
			    Configuration config=new Configuration();
		        String rmiAddress=config.getRmiAddress();
		        LocationInterface currentLocationName = null;
		        currentLocationName=(LocationInterface) Naming.lookup(rmiAddress+"Location");
		        name = currentLocationName.getName( person.getCurrentLocationNum());
			 }catch(RemoteException e4){
			    	e4.printStackTrace();
			    } catch (MalformedURLException e5) {
			    	// TODO Auto-generated catch block
			    	e5.printStackTrace();
			    } catch (NotBoundException e6) {
			    	// TODO Auto-generated catch block
			    	e6.printStackTrace();
			    }
			contentPanel.setLayout(null);
			JLabel lblNewLabel_3 = new JLabel(name);

			lblNewLabel_3.setForeground(new Color(0, 0, 255));
			lblNewLabel_3.setFont(new Font("微软雅黑", Font.BOLD, 20));
			lblNewLabel_3.setBounds(236, 26, 152, 48);

			contentPanel.add(lblNewLabel_3);
		
			JLabel label = new JLabel("\u5230\u7AD9");
			label.setBounds(115, 90, 93, 28);
			label.setForeground(new Color(255, 255, 0));
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setFont(new Font("微软雅黑", Font.BOLD, 20));
			contentPanel.add(label);
		
		
			JLabel lblNewLabel = new JLabel(a);
			lblNewLabel.setBounds(236, 80, 152, 48);
			lblNewLabel.setForeground(new Color(255, 255, 0));
			lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));

			contentPanel.add(lblNewLabel);
		
		
			JLabel label2 = new JLabel("\u7968\u4EF7");
			label2.setBounds(115, 158, 93, 28);
			label2.setForeground(Color.CYAN);
			label2.setHorizontalAlignment(SwingConstants.CENTER);
			label2.setFont(new Font("微软雅黑", Font.BOLD, 20));
			contentPanel.add(label2);

			JTextField textField = new JTextField(b+"");
			textField.setForeground(Color.CYAN);
			textField.setHorizontalAlignment(SwingConstants.RIGHT);
			textField.setFont(new Font("方正舒体", Font.BOLD, 20));
			textField.setBounds(248, 158, 56, 28);
			contentPanel.add(textField);
			textField.setColumns(10);

			

			JLabel lblNewLabel_1 = new JLabel("元");
			lblNewLabel_1.setBounds(314, 153, 50, 39);
			lblNewLabel_1.setForeground(Color.CYAN);
			lblNewLabel_1.setFont(new Font("微软雅黑", Font.BOLD, 20));

			contentPanel.add(lblNewLabel_1);
			
			
		
		
		JButton button_ok = new JButton("\u786E\u5B9A\u652F\u4ED8");
		button_ok.setBounds(115, 218, 93, 33);
		if(c == true ){
		    button_ok.addActionListener(new ActionListener() {
			    public void actionPerformed(ActionEvent e) {				
			    	SubSuccess dialog = new SubSuccess();
			    	dialog.setLocationRelativeTo(null);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				    dispose();	
				    person.setCurrentLocationNum( d );
				    
				    try{
				    Configuration config=new Configuration();
			        String rmiAddress=config.getRmiAddress();
			        LocationInterface currentLocationName = null;
			        currentLocationName=(LocationInterface) Naming.lookup(rmiAddress+"Location");
			        String name = currentLocationName.getName( d);
				    Map.lblNewLabel.setText(name);
				    }catch(RemoteException e3){
				    	e3.printStackTrace();
				    }catch (MalformedURLException e4) {
				    	// TODO Auto-generated catch block
				    	e4.printStackTrace();
				    	} catch (NotBoundException e5) {
				    	// TODO Auto-generated catch block
				    	e5.printStackTrace();
				    	}
				}
		    });
		}
		else{
			button_ok.addActionListener(new ActionListener() {
			    public void actionPerformed(ActionEvent e) {
			    	SubFail dialog = new SubFail();
			    	dialog.setLocationRelativeTo(null);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				    dispose();					    
				}
		    });
		}
		contentPanel.add(button_ok);
		
		JButton button_cancel = new JButton("\u53D6\u6D88");
		button_cancel.setBounds(284, 218, 93, 33);
		button_cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SubName subname=new SubName();
				subname.setLocationRelativeTo(null);
				subname.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				subname.setVisible(true);
				dispose();
			}
		});
		contentPanel.add(button_cancel);
		
		JLabel lblNewLabel_2 = new JLabel("\u672C\u7AD9");

		lblNewLabel_2.setForeground(new Color(0, 0, 255));
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.BOLD, 20));

		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);

		lblNewLabel_2.setBounds(115, 36, 93, 28);

		contentPanel.add(lblNewLabel_2);

		
		ImageIcon im = new ImageIcon("src/client/images/ticket.png");
	    JLabel  l1 = new JLabel(im);
	    l1.setBounds(0, 0, 484, 312);
	    contentPanel.add(l1);
	    

												
	}
}
